package libra.baseConfig;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.catalina.servlets.DefaultServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.util.WebUtils;


/**
 * 注册Servlet3大组件
 * @author libra
 */
@Configuration
public class ServletConfig {
	//不需要被过滤的静态资源type
    public static final String[] static_res_type = {
    		// JS
    		".js",".css",
    		// Images
    		".jpg",".png",".gif",".svg",".bmp",".ico",
    		// 软件
    		".exe",
    		// 字体
    		".map",".woff",".woff2",".ttf",".eot" };
    //不需要被过滤的静态资源url
    public static final String[] static_res_url_pattern = Arrays.stream(static_res_type)
    														.map( ( type ) -> { return "*"+type; } )
    														.collect( Collectors.toList() )
    														.toArray( new String[] {} );
	
	/*
	 * 注册Servlet 
	 */
//	@Autowired
//	DefaultServlet defaultServlet;
	
	@Bean
	public ServletRegistrationBean<DefaultServlet> myServletRegistrationBean3() {
		DefaultServlet ds = new DefaultServlet();
		ServletRegistrationBean<DefaultServlet> myServletRegistrationBean = new ServletRegistrationBean<DefaultServlet>(ds, static_res_url_pattern);
		return myServletRegistrationBean;
	}
	
	/*
	 * 注册Filter
	 */
	@Bean
	public FilterRegistrationBean<Filter> myFilterRegistrationBean1() {
		FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
		filterRegistrationBean.setFilter(new Filter() {
			@Override
			public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
					throws IOException, ServletException {
			}
		});
		// “/myServlet1”手动注册的servlet，“/test/test”容器中的controller下的方法。拦截器不作用servlet
		filterRegistrationBean.setUrlPatterns(Arrays.asList("/manualServlet","/test/test"));
		return filterRegistrationBean;
	}

	
	/*
	 * 注册Listener
	 static {
		Set<Class<?>> types = new HashSet<Class<?>>();
		types.add(ServletContextAttributeListener.class);
		types.add(ServletRequestListener.class);
		types.add(ServletRequestAttributeListener.class);
		types.add(HttpSessionAttributeListener.class);
		types.add(HttpSessionListener.class);
		types.add(ServletContextListener.class);
		SUPPORTED_TYPES = Collections.unmodifiableSet(types);
	}
	 */
	@Bean
	public ServletListenerRegistrationBean<ServletContextListener> myServletListenerRegistrationBean(){
		ServletListenerRegistrationBean<ServletContextListener> ServletContextListener = new ServletListenerRegistrationBean<>();
		ServletContextListener.setListener( new ServletContextListener() {
			@Override
			public void contextInitialized(ServletContextEvent sce) {
				// TODO Auto-generated method stub
				ServletContextListener.super.contextInitialized(sce);
			}
			@Override
			public void contextDestroyed(ServletContextEvent sce) {
				// TODO Auto-generated method stub
				ServletContextListener.super.contextDestroyed(sce);
			}
		} );
		return ServletContextListener;
	}
}
